package dhee.wtqshopproject.dao;

import dhee.wtqshopproject.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {


 // ===== VIP会员专属方法 =====
 // 查询所有VIP会员（不分页）
 @Select("SELECT * FROM hxshop.user WHERE role = 'vip'")
 List<User> selectAllVipUsers();

 // VIP会员分页查询
 @Select("SELECT * FROM hxshop.user WHERE role = 'vip' LIMIT #{offset}, #{pageSize}")
 List<User> selectVipUsersByPage(@Param("offset") int offset,
                                 @Param("pageSize") int pageSize);

 // 统计VIP会员总数（用于分页）
 @Select("SELECT COUNT(*) FROM hxshop.user WHERE role = 'vip'")
 int countVipUsers();

 // VIP会员关键字搜索（分页）
 @Select("SELECT * FROM hxshop.user "
         + "WHERE role = 'vip' "
         + "AND (username LIKE CONCAT('%', #{keyword}, '%') "
         + "OR phone LIKE CONCAT('%', #{keyword}, '%') "
         + "OR email LIKE CONCAT('%', #{keyword}, '%')) "
         + "LIMIT #{offset}, #{pageSize}")
 List<User> searchVipUsers(@Param("keyword") String keyword,
                           @Param("offset") int offset,
                           @Param("pageSize") int pageSize);



 // 统计VIP会员搜索结果数量
 @Select("SELECT COUNT(*) FROM hxshop.user "
         + "WHERE role = 'vip' "
         + "AND (username LIKE CONCAT('%', #{keyword}, '%') "
         + "OR phone LIKE CONCAT('%', #{keyword}, '%') "
         + "OR email LIKE CONCAT('%', #{keyword}, '%'))")
 int countSearchVipUsers(@Param("keyword") String keyword);
   //注册
   @InsertProvider(type = UserSqlProvider.class, method = "insertUserSql")
    void zhuceUser(User user);
    // 动态用户登录
    @InsertProvider(type = UserSqlProvider.class, method = "LoginSql")
    void login(User user);
    // 新增用户
    @Insert("INSERT INTO hxshop.user (username, password,sex, email, phone, avatar_url, create_time, role, is_active) " +
            "VALUES (#{username}, #{password}, #{sex},#{email}, #{phone}, #{avatarUrl}, #{createTime}, #{role}, #{isActive})")
    int insertUser(User user);
    // 根据ID删除用户
    @Delete("DELETE FROM hxshop.user WHERE user_id = #{id}")
    int deleteById(@Param("id") Integer id);
    // 更新用户信息
    @Update("UPDATE hxshop.user SET " +
            "username = #{user.username}, " +
            "password = #{user.password}, " +
            "sex=#{user.sex},"+
            "email = #{user.email}, " +
            "phone = #{user.phone}, " +
            "avatar_url = #{user.avatarUrl}, " +
            "role = #{user.role}, " +
            "is_active = #{user.isActive} " +
            "WHERE user_id = #{user.userId}")
    int updateUser(@Param("user") User user);
    // 根据ID查询用户
    @Select("SELECT * FROM hxshop.user WHERE user_id = #{id}")
    User selectById(@Param("id") Integer id);
    @Select("select *from user where username=#{username}")
    List<User> selectByUsername(@Param("username") String username);

    // 通用查询（可选）
    @Select("SELECT * FROM hxshop.user")
    List<User> selectAll();
   //模糊搜索
    @Select("select * from user where username LIKE CONCAT('%', #{keyword}, '%')OR phone LIKE CONCAT('%',#{keyword},'%')OR email LIKE CONCAT('%',#{keyword},'%')         ")
            List<User> selectLike(String keyword);
}